package com.eqdd.yiqidian.http

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.shizhefei.mvc.ILoadViewFactory
import com.shizhefei.view.vary.VaryViewHelper

/**
 *
 * Created by jhj on 18-7-25.
 */
class CustomLoadViewFactory : ILoadViewFactory {

    private var isShowEmpty: Boolean = true
    private var isShowFooter: Boolean = true

    override fun madeLoadMoreView(): ILoadViewFactory.ILoadMoreView {
        return LoadMoreHelper()
    }

    override fun madeLoadView(): ILoadViewFactory.ILoadView {
        return LoadViewHelper()
    }


    fun setShowEmpty(isShowEmpty: Boolean): CustomLoadViewFactory {
        this.isShowEmpty = isShowEmpty
        return this
    }

    fun setShowFooter(isShowFooter: Boolean): CustomLoadViewFactory {
        this.isShowFooter = isShowFooter
        return this
    }


    inner class LoadMoreHelper : ILoadViewFactory.ILoadMoreView {

        private var footView: TextView? = null
        private var onClickRefreshListener: View.OnClickListener? = null


        override fun init(footViewHolder: ILoadViewFactory.FootViewAdder, onClickRefreshListener: View.OnClickListener) {
            val contentView = footViewHolder.contentView

            val context = contentView.context
            val textView = TextView(context)
            textView.setTextColor(Color.GRAY)
            textView.setPadding(0, dip2px(context, 16f), 0, dip2px(context, 16f))
            textView.gravity = Gravity.CENTER
            footViewHolder.addFootView(textView)

            footView = textView
            this.onClickRefreshListener = onClickRefreshListener
            showNormal()
        }

        override fun showNormal() {
            if (isShowFooter) {
                footView?.text = "点击加载更多"
            }
            footView?.setOnClickListener(onClickRefreshListener)
        }

        override fun showLoading() {
            if (isShowFooter) {
                footView?.text = "正在加载中.."
            }
            footView?.setOnClickListener(null)

        }

        override fun showFail(exception: Exception) {
            if (isShowFooter) {
                footView?.text = "加载失败，点击重新加载"
            }
            footView?.setOnClickListener(onClickRefreshListener)

        }

        override fun showNomore() {
            if (isShowFooter) {
                footView?.text = "已经加载完毕"
            }
            footView?.setOnClickListener(null)

        }

    }

    inner class LoadViewHelper : ILoadViewFactory.ILoadView {
        private var helper: VaryViewHelper? = null
        private var onClickRefreshListener: View.OnClickListener? = null
        private var context: Context? = null


        override fun init(switchView: View, onClickRefreshListener: View.OnClickListener) {
            this.context = switchView.context.applicationContext
            this.onClickRefreshListener = onClickRefreshListener
            helper = VaryViewHelper(switchView)
        }

        override fun restore() {
            helper?.restoreView()
        }

        override fun showLoading() {
            val context = helper?.context

            val layout = LinearLayout(context)
            layout.orientation = LinearLayout.VERTICAL
            layout.gravity = Gravity.CENTER

            val progressBar = ProgressBar(context)
            layout.addView(progressBar)

            val textView = TextView(context)
            textView.text = "加载中..."
            textView.gravity = Gravity.CENTER
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            val top = dip2px(context, 12f)
            params.setMargins(0, top, 0, 0)
            layout.addView(textView, params)

            helper?.showLayout(layout)
        }

        override fun tipFail(exception: Exception) {
            Toast.makeText(context, "网络加载失败", Toast.LENGTH_SHORT).show()
        }

        override fun showFail(exception: Exception) {
            val context = helper?.context

            val layout = LinearLayout(context)
            layout.orientation = LinearLayout.VERTICAL
            layout.gravity = Gravity.CENTER

            val textView = TextView(context)
            textView.text = "网络加载失败"
            textView.gravity = Gravity.CENTER
            layout.addView(textView)

            val button = Button(context)
            button.text = "重试"
            button.setOnClickListener(onClickRefreshListener)
            val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            val top = dip2px(context, 12f)
            params.setMargins(0, top, 0, 0)
            layout.addView(button, params)

            helper?.showLayout(layout)
        }

        override fun showEmpty() {

            val layout = LinearLayout(context)
            if (isShowEmpty) {
                val context = helper?.context

                layout.orientation = LinearLayout.VERTICAL
                layout.gravity = Gravity.CENTER

                val textView = TextView(context)
                textView.text = "暂无数据"
                textView.gravity = Gravity.CENTER
                layout.addView(textView)

                val button = Button(context)
                button.text = "重试"
                button.setOnClickListener(onClickRefreshListener)
                val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                val top = dip2px(context, 12f)
                params.setMargins(0, top, 0, 0)
                layout.addView(button, params)
            }
            helper?.showLayout(layout)

        }

    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    fun dip2px(context: Context?, dpValue: Float): Int {
        val scale = context?.resources?.displayMetrics?.density ?: 0f
        return (dpValue * scale + 0.5f).toInt()
    }
}